package com.tybbank.Activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tybbank.Base.BaseActivity;
import com.tybbank.Bean.RollOutOneBean;
import com.tybbank.Login.LoginActivity;
import com.tybbank.MyApplication;
import com.tybbank.R;
import com.tybbank.Utils.Constant;
import com.tybbank.Utils.MD5Utils;
import com.tybbank.Utils.MyDialogUtils;
import com.tybbank.Utils.logUtilssss;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.Comparator;
import java.util.SortedMap;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * Created by Administrator on 2018/9/26 0026.
 */

public class RollOutOneActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.et_account)
    EditText etAccount;
    @BindView(R.id.bt_enter)
    Button btEnter;
    @BindView(R.id.bt_init_enter)
    Button btInitEnter;
    private Intent intent;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rolloutone);
        ButterKnife.bind(this);
        toolbar(toolbar, getString(R.string.zhuanchu), R.mipmap.icon_close, getString(R.string.jilu));
    }

    @OnClick({R.id.bt_enter,R.id.bt_init_enter})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_init_enter:
                Intent intent = new Intent(this, IntoActivity.class);
              startActivity(intent);
                break;
            case R.id.bt_enter:
                if (TextUtils.isEmpty(etAccount.getText().toString().trim())) {
                    showTips(getString(R.string.shuruuuid));
                } else {
                    init();
                }
                break;
        }
    }

    private void init() {
        String time = MD5Utils.getTime();
        SortedMap<String, String> parameters = new TreeMap<String, String>(
                new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        // 降序排序
                        return obj1.compareTo(obj2);
                    }
                });
        parameters.put("uuid", MyApplication.uuid);
        parameters.put("token", MyApplication.token);
        parameters.put("type", "2");
        parameters.put("account", etAccount.getText().toString().trim());
        parameters.put("TimeStamp", time);
        String sign = MD5Utils.createSign(parameters);
        initdata(time, sign);

    }

    private void initdata(String time, String sign) {
        final Dialog dialog = MyDialogUtils.createLoadingDialog(this, getString(R.string.loading));
        dialog.show();
        OkHttpUtils
                .post()
                .url(Constant.verify_identity)
                .addParams("uuid", MyApplication.uuid)
                .addParams("token", MyApplication.token)
                .addParams("type", "2")
                .addParams("account", etAccount.getText().toString().trim())
                .addParams("TimeStamp", time)
                .addParams("Sign", sign)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                        dialog.dismiss();
                        showTips(getString(R.string.error));
                        return;
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        logUtilssss.e("TAG根据手机号或UUID查询用户是否存在" + response.toString());
                        dialog.dismiss();
                        string(response);
                    }

                    private void string(String json) {
                        try {
                            RollOutOneBean enterBean = new Gson().fromJson(json, new TypeToken<RollOutOneBean>() {
                            }.getType());
                            int status = enterBean.getCode();
                            if (status == 1011) {
                                RollOutOneBean<RollOutOneBean.DataBean> enter = new Gson().fromJson(json, new TypeToken<RollOutOneBean<RollOutOneBean.DataBean>>() {
                                }.getType());
                                intent = new Intent(getBaseContext(), RollOutTwoActivity.class);    // 输入姓名验证是否存在 跳往转账页面
                                intent.putExtra("etAccount", enter.getData().getAccount());
                                intent.putExtra("uuid", enter.getData().getUuid());
                                startActivityForResult(intent, 101);
                                setResult(RESULT_OK);
                            } else if (status == 1004) {
                                intent = new Intent(getBaseContext(), LoginActivity.class);
                                startActivity(intent);
                                return;
                            } else {
                                showTips(enterBean.getMsg());
                            }
                        } catch (Exception e) {
                            showTips(getString(R.string.error));
                        }

                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 101) {
            setResult(RESULT_OK);
        }
    }

    @Override
    public void RightNext() {
        super.RightNext();
        intent = new Intent(this, ZhuanChuActivity.class);
        intent.putExtra("type", "3");
        startActivity(intent);
    }

    @Override
    public void LeftClose() {
        super.LeftClose();
        RollOutOneActivity.this.finish();
    }

}
